package com.gxnu.edu.cn.ttxxt.service.Impl;

import com.gxnu.edu.cn.ttxxt.entity.Messages;
import com.gxnu.edu.cn.ttxxt.mapper.MessageRepository;
import com.gxnu.edu.cn.ttxxt.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageRepository messageRepository;

    @Override
    @Transactional
    public Messages sendTextMessage(Long senderId, Long receiverId, String content, Integer isGroup) {
        // 参数校验
        if (senderId == null || receiverId == null || content == null) {
            throw new IllegalArgumentException("发送者、接收者和内容不能为空");
        }

        Messages messages = new Messages();
        messages.setSenderId(senderId);
        messages.setReceiverId(receiverId);
        messages.setMessageType(1);       // 1-文本消息
        messages.setContent(content);
        messages.setIsGroup(isGroup);
        messages.setSendTime(new Date()); // 发送时间
        messages.setReceiveTime(new Date()); // 接收时间
        messages.setStatus(2);           // 2-已发送

        System.out.println("插入前: " + messages); // 此时messageId应为null

        // 关键修改：接收返回值
        messageRepository.insert(messages);
 // 检查messageId是否已填充

        return messages; // 返回数据库操作后的对象
    }

    @Override
    @Transactional
    public Messages sendFileMessage(Long senderId, Long receiverId, String content, Integer isGroup, Long fileId) {
        Messages messages = new Messages();
        messages.setSenderId(senderId);
        messages.setReceiverId(receiverId);
        messages.setMessageType(2); // 2-文件消息
        messages.setContent(content);
        messages.setFileId(fileId);
        messages.setIsGroup(isGroup);
        messages.setSendTime(new Date());
        messages.setStatus(2); // 2-已发送

        messageRepository.insert(messages);
        return messages;
    }

    @Override
    @Transactional
    public Messages sendShakeMessage(Long senderId, Long receiverId, Integer isGroup) {
        Messages messages = new Messages();
        messages.setSenderId(senderId);
        messages.setReceiverId(receiverId);
        messages.setMessageType(3); // 3-抖动消息
        messages.setIsGroup(isGroup);
        messages.setSendTime(new Date());
        messages.setStatus(2); // 2-已发送

        messageRepository.insert(messages);
        return messages;
    }

    @Override
    public List<Messages> getChatHistory(Long userId, Long contactId, Integer isGroup, int page, int size) {
        int offset = (page - 1) * size;
        return messageRepository.findChatHistory(userId, contactId, isGroup, offset, size);
    }

    @Override
    public List<Messages> getUnreadMessages(Long userId) {
        return messageRepository.findByReceiverIdAndStatus(userId, 3); // 3-已接收但未读
    }

    @Override
    @Transactional
    public void markMessagesAsRead(Long userId, Long contactId, Integer isGroup) {
        messageRepository.updateMessagesAsRead(userId, contactId, isGroup);
    }
}